﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename : D_PowerGroupExp.cs
//        description :
//
//        created by 周济 at 2010/8/22 16:19:52
//        mail:13912699233@139.com
//
//======================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ksyx.User.Dal
{
    public class D_PowerGroupExp : D_PowerGroup
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsExp(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_PowerGroup");
            strSql.Append(" where id=@id and flag=1 ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return ksyx.DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 如果有用户,则返回false,没则返回true
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsUserCoun(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select count(a.id)  ");
            strSql.Append(" from  dbo.T_PowerGroupSon a  ");
            strSql.Append(" inner join t_adminuser b on a.userid=b.id ");
            strSql.Append(" where a.powergroupid=@id and a.flag=1 and b.flag=1 ");

            var item = strSql.ToString().Exp_GetSingle(YXSQL.SQLConType.C1, new SqlParameter("@id", SqlDbType.Int) { Value = id });
            var intRefite = item.Exp_IntTryParse();
            return !(intRefite > 0);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int AddExp(ksyx.User.Model.T_PowerGroup model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PowerGroup(");
            strSql.Append("PowerGroupName,Explain,Flag)");
            strSql.Append(" values (");
            strSql.Append("@PowerGroupName,@Explain,@Flag)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@PowerGroupName", SqlDbType.NVarChar,500),
					new SqlParameter("@Explain", SqlDbType.NVarChar,500),
					new SqlParameter("@Flag", SqlDbType.Bit,1)};
            parameters[0].Value = model.PowerGroupName;
            parameters[1].Value = model.Explain;
            parameters[2].Value = model.Flag;

            object obj = ksyx.DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int UpdateExp(ksyx.User.Model.T_PowerGroup model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PowerGroup set ");
            strSql.Append("PowerGroupName=@PowerGroupName,");
            strSql.Append("Explain=@Explain,");
            strSql.Append("Flag=Flag");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@PowerGroupName", SqlDbType.NVarChar,500),
					new SqlParameter("@Explain", SqlDbType.NVarChar,500),
					new SqlParameter("@Flag", SqlDbType.Bit,1)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.PowerGroupName;
            parameters[2].Value = model.Explain;
            parameters[3].Value = model.Flag;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        public int DelExp(int id)
        {
            return " T_PowerGroup ".Exp_UpdateFlag(id, YXSQL.SQLConType.C1);
        }
        public int DelExp(List<int> list)
        {
            return " T_PowerGroup ".Exp_UpdateFlag(list, YXSQL.SQLConType.C1);
        }

        public List<Model.T_PowerGroupExp> GetListPage(int b, int p, string Name)
        {
            //           select g.id,
            //g. powergroupname,
            //userCount=( 
            //    select count(a.id) 
            //    from  dbo.T_PowerGroupSon a 
            //    inner join t_adminuser b on a.userid=b.id 
            //    where a.powergroupid=g.id and a.flag=1 and b.flag=1 ),
            //pocount=(select count(a.id) from dbo.T_Power a where a.powergroupid=g.id and a.flag=1)
            //from t_powergroup  g
            //where g.flag=1 

            var strSql = ExpandMSSQL.GetPageSQL(b, p,
                " g.id,g. powergroupname," +
                    "userCount=( select count(a.id) from  dbo.T_PowerGroupSon a inner join t_adminuser b on a.userid=b.id where a.powergroupid=g.id and a.flag=1 and b.flag=1 )," +
                    "pocount=(select count(a.id) from dbo.T_Power a where a.powergroupid=g.id and a.flag=1) ",
                " t_powergroup  g ",
                " g.id ",
                " g.powergroupname like '%'+@Name+'%'",
                " g.id desc  ");
            var list = strSql.Exp_GetList(YXSQL.SQLConType.C1,
                    DR => new Model.T_PowerGroupExp()
                    {
                        PowerGroupName = DR["powergroupname"].Exp_Trim(),
                        PowerCount = DR["pocount"].Exp_IntTryParse(),
                        Flag = true,
                        id = DR["id"].Exp_IntTryParse(),
                        UserCount = DR["userCount"].Exp_IntTryParse(),
                        Explain = ""
                    },
                    new SqlParameter("@Name", SqlDbType.NVarChar, 30) { Value = Name });

            return list;

        }
        public int GetListPage(string Name)
        {
            var strSql = ExpandMSSQL.GetPageSQL(
                " t_powergroup  g ",
                " g.powergroupname like '%'+@Name+'%'"
                );
            var intRef = strSql.Exp_GetSingle(
                    new SqlParameter("@Name", SqlDbType.NVarChar, 30) { Value = Name });
            return intRef.Exp_IntTryParse();
        }
        public List<Model.T_PowerGroup> GetList()
        {
            string strSql = "  SELECT     id, PowerGroupName, Explain, Flag FROM         dbo.T_PowerGroup where flag=1 order by id";

            var list = strSql.Exp_GetList(YXSQL.SQLConType.C1,
                    DR => new Model.T_PowerGroup()
                    {
                        PowerGroupName = DR["powergroupname"].Exp_Trim(),
                        Flag = true,
                        id = DR["id"].Exp_IntTryParse(),
                        Explain = ""
                    });

            return list;

        }
    }
}
